package com.wfs.myapp.utils;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class HttpUtil {

    private final OkHttpClient client;

    private final MediaType JSON = MediaType.get("application/json; charset=utf-8");
    public static final MediaType FORM = MediaType.parse("multipart/form-data");

//    private String WX_TOKEN = "";

    public HttpUtil() {
        // 创建OkHttpClient的Builder对象
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        // 设置连接超时时间为10秒
        builder.connectTimeout(30, TimeUnit.SECONDS);
        // 设置读取超时时间为20秒
        builder.readTimeout(30, TimeUnit.SECONDS);
        // 设置写入超时时间为30秒
        builder.writeTimeout(30, TimeUnit.SECONDS);
        this.client = builder.build();
    }

    public String get(String url, Map<String, String> headers) {
        Request request = new Request.Builder()
                .url(url)
                .headers(Headers.of(headers))
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String get(String url) {
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String post(String url,Map<String, String> headers, Object obj) {
        RequestBody body = RequestBody.create(JSON, JSONObject.toJSONString(obj));
        Request request = new Request.Builder()
                .url(url)
                .headers(Headers.of(headers))
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public byte[] postBody(String url,Map<String, String> headers, Object obj) {
        RequestBody body = RequestBody.create(JSON, JSONObject.toJSONString(obj));
        Request request = new Request.Builder()
                .url(url)
                .headers(Headers.of(headers))
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().bytes();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public String post(String url, Object obj) {
        RequestBody body = RequestBody.create(JSON, JSONObject.toJSONString(obj));
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}